#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
        if (s.empty())
            return 0;
        vector<int> ans(128, 0);
        int max = -1;
        int len = 0;
        for (int left = 0, right = 0; right < s.size(); right++)
        {
            if (ans[s[right]] == 1)
            {
                max = max > len ? max : len;
                while (s[left] != s[right])
                {
                    ans[s[left]]--;
                    left++;
                    len--;
                }
                left++;
                continue;
            }
            ans[s[right]]++;
            len++;
            max = max > len ? max : len;
        }
        return max;
    }
};